package com.xiaohei.rabbitmq.three;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import com.xiaohei.rabbitmq.utils.RabbitmqUtils;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @Description: 消息在手动应答时不丢失
 * @Author: 王希辰
 * @Date: 2022/3/17 18:17
 */
public class Task2 {
    //队列名称
    public static final String TASK_QUEUE_NAME="ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitmqUtils.getChannel();
        //声明队列
        channel.queueDeclare
                (TASK_QUEUE_NAME,false,false,
                        false,null);
        //开启发布确认
        channel.confirmSelect();
        //从控制台输入
        Scanner scanner=new Scanner(System.in);
        while (scanner.hasNext()){
            String m = scanner.next();
            //设置生产者发送消息为持久化消息（要求保存到磁盘上） 一般保存在内存
            //MessageProperties.PERSISTENT_TEXT_PLAIN
            channel.basicPublish("",TASK_QUEUE_NAME,
                    MessageProperties.PERSISTENT_TEXT_PLAIN,m.getBytes(StandardCharsets.UTF_8));
            System.out.println("生产者发出消息："+m);
        }
    }

}







